package com.baizhi.action;

import com.baizhi.entity.Food;
import com.baizhi.service.FoodService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FoodAction {
    private int pageNum;
    private int pageSize;
    private int foodId;//删除需要的ID
    private Food food;//添加，修改需要的food对象
    private FoodService fs;
    //分页展示所有食物
    public void show() throws IOException {
        List<Food> foods = fs.selectFoods(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        PageInfo<Food> pageInfo = new PageInfo<>(foods,3);
        int[] pages = pageInfo.getNavigatepageNums();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        map.put("foods",foods);
        map.put("pages",pages);
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    //删除当前选中的食物
    public void deleteFood() throws IOException {
        Map<String, Object> map = new HashMap<>();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        try {
            fs.deleteFoodById(foodId);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    //添加食物
    public void insertFood() throws IOException {
        Map<String, Object> map = new HashMap<>();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        try {
            fs.insertFood(food);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    //修改食物
    public void selectFoodById() throws IOException {
        Map<String, Object> map = new HashMap<>();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        food = fs.selectFoodById(foodId);
        map.put("food",food);

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    public void updateFood() throws IOException {
        Map<String, Object> map = new HashMap<>();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        try {
            fs.updateFood(food);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    public int getPageNum() {
        return pageNum;
    }
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public void setFs(FoodService fs) {
        this.fs = fs;
    }
    public int getFoodId() {
        return foodId;
    }
    public void setFoodId(int foodId) {
        this.foodId = foodId;
    }
    public Food getFood() {
        return food;
    }
    public void setFood(Food food) {
        this.food = food;
    }
}
